package cn.richard.databind.viewmodel

import android.os.CountDownTimer
import android.util.Log
import androidx.databinding.Observable
import androidx.lifecycle.ViewModel
import cn.richard.databind.model.UserModel
import java.util.*

/**
 *
 * @ProjectName: CaseApp
 * @Author: wuzhiguo@aiforward.com
 * @CreateDate: 2021/8/27 4:48 下午
 * @Description:
 */
class UserViewModel : ViewModel(){

    private var userModel: UserModel? = null
    private var timer: CountDownTimer? = null

    fun setUserModel(userModel: UserModel?){
        this.userModel = userModel
        this.userModel?.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                //监听之后做其他操作
            }
        })
    }

    fun getUserInfo() {
        timer?.cancel()
        timer = object : CountDownTimer(100 * 1000L, 1000) {
            override fun onTick(feature: Long) {

                userModel?.age = (userModel?.age?:0)+1
                userModel?.name = "richard_${userModel?.age}"
                Log.i("CountDownTimer", "age == " + userModel?.age)
            }

            override fun onFinish() {

            }
        }
        timer?.start()
    }

    override fun onCleared() {
        super.onCleared()
        timer?.cancel()
        timer = null
    }

}